package com.hurl.waguji.biz.download;

import java.util.Date;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;

import com.hurl.library.spider.SpiderException;
import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.DateUtils;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.DateService;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCjmxFjmxHexunBO;
import com.hurl.waguji.persistence.JcsjCjmxFjmxHexunBOId;

class MrhqCjmxFjmxHexunProcessor extends BaseDownloadProcessor{
	public MrhqCjmxFjmxHexunProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
		context.setBoSaveMode(DownloadContext.BO_SAVE_MODE_INSERT);
	}
	private static final Logger log = Logger.getLogger(MrhqCjmxFjmxHexunProcessor.class);

	private Date lastTradingTime;
	private Date lastTradingDate;

	@Override
	public void downloadFailure(String url, Throwable e) throws SpiderException {
		throw new SpiderException("下载错误，停止下载！",e);
	}

	@Override
	public Date getRq() {
		return lastTradingDate;
	}

	@Override
	public void validate() throws Throwable {

		lastTradingTime = DateService.getLastTradingTimeFromWeb();
		lastTradingDate = DateUtils.getDate(this.lastTradingTime);
		
		/* 只能在交易日当天休市后才能下载 */
		validateNowAfterTradingTime(lastTradingTime);
	}

	@Override
	public boolean beforeDownload(String url) throws SpiderException {
		/* 下载过的项就不要重复下载了 */
		int size = context.getDao().queryBySQL(SqlReader.getSql("SQL12041401", new String[]{context.getEnvAsString("gpdm"),DateUtils.formatDate(getRq(), "yyyy-MM-dd")})).list().size();
		if(size >0){
			log.debug(context.getEnvAsString("gpdm")+"已经下载过！");
			return false;
		}
		return true;
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isEmpty(html) || html.contains("nostock")){
			return ;
		}
		
		String gpdm = StringUtils.substringBetween(url, "gpdm=", null);
		
		try {
			JSONArray jsonarray = null;
			jsonarray = new JSONArray(html);
			if(jsonarray==null || jsonarray.length()<2){
				return ;
			}
			JSONArray obj1 = (JSONArray)jsonarray.get(0);
			String gpmc = Formatter.toString(obj1.getString(1));
			Date gxrq = Formatter.toDate(obj1.getString(2),"yyyy年MM月dd日 HH:mm");
			lastTradingDate = DateUtils.getDate(gxrq);
			JcsjCjmxFjmxHexunBO bo = null;
			for(int i=1; i<jsonarray.length();i++){
				JSONArray item = (JSONArray)jsonarray.get(i);
				bo = new JcsjCjmxFjmxHexunBO();
				bo.setId(new JcsjCjmxFjmxHexunBOId(gpdm,lastTradingDate,Formatter.toDouble(item.getString(0))));
				bo.setGpmc(gpmc);
				bo.setMrcjje(Formatter.toDouble(item.getString(1))*10000);
				bo.setMrcjbs(Formatter.toInteger(item.getString(2)));
				bo.setMccjje(Formatter.toDouble(item.getString(3))*10000);
				bo.setMccjbs(Formatter.toInteger(item.getString(4)));
				bo.setMrmcce(Formatter.toDouble(item.getString(5))*10000);
				bo.setMrmchj(Formatter.toDouble(item.getString(6))*10000);
				bo.setQtcj(Formatter.toDouble(item.getString(7))*10000);
				bo.setZqrcjbl(Formatter.toDouble(item.getString(8))/100);
				bo.setGxrq(gxrq);
				collector.addBO(bo);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			context.setStopWithError(e);
		}
			
	}

	@Override
	public void shutdown(boolean success, Throwable t) {
		if(!success){
		}
	}
	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://vol.stock.hexun.com/Now/Share/PriceDetail_1.ashx?code="+bo.getGpDmJ()+"&gpdm="+bo.getGpDm();
			}
		};
	}

}
